﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using SlamCms.Common;

namespace SlamCms.Services
{
	[EditorBrowsable(EditorBrowsableState.Never)]
	public static class DependencyResolverExtensions
	{
		public static TService GetService<TService>(this IDependencyResolver dependencyResolver)
		{
			if (dependencyResolver == null)
				throw Error.ArgumentNull("services");

			return (TService)dependencyResolver.GetService(typeof(TService));
		}

		public static IEnumerable<TService> GetServices<TService>(this IDependencyResolver dependencyResolver)
		{
			if (dependencyResolver == null)
				throw Error.ArgumentNull("services");

			return dependencyResolver.GetServices(typeof(TService)).Cast<TService>();
		}

		public static T GetServiceOrThrow<T>(this IDependencyResolver dependencyResolver)
		{
			T result = dependencyResolver.GetService<T>();

			if (result == null)
				throw Error.InvalidOperation("There is not service for {0}", typeof(T).FullName);

			return result;
		}
	}
}